<template>
    <div class="common-layout">
        <el-row>
            <el-table :data="state.tableData" 
            :cell-class-name="tableColumnClassName"
            @sort-change="sortChange"
            :default-sort="state.sort"
            @header-click="tableHeaderClick"
            style="width: 100%" height="800">
                <el-table-column prop="date" fixed label="Date" width="200" sortable/>
                <el-table-column 
                    v-for="stock in state.stocks" width="100"
                    :key="stock" 
                    :prop="stock" 
                    :label="stock"/>

            </el-table>
        </el-row>
    </div>
</template>
  
<script setup>
import { reactive } from 'vue'
import { getMultiWeekData } from '../service/week';

import router, { openNewTableWeekDetail } from '../router'
import { getLpClassName } from '../utils/help';

const base = "MultiWeekLpDetail"
const sortPropKey = base + "SortProp"
const sortOrderKey = base + "SortOrder"

const prop = defineProps({
    stocks: String
})

        const state = reactive({
            data: [],
            raw_data: null,
            tableData: [],
            stocks: [],
            list_data : [],
            options: [],
            sort: { 
                prop: sessionStorage.getItem(sortPropKey) || 'date' , 
                order: sessionStorage.getItem(sortOrderKey) || 'descending' 
            },
        })

        const sortChange = (column, prop, order) => {
            sessionStorage.setItem(sortPropKey, column.prop)
            sessionStorage.setItem(sortOrderKey, column.order)
        }

        const loadData = () => {
            getMultiWeekData(prop.stocks).then(res => {

                state.raw_data = res.data
                state.stocks = Object.keys(state.raw_data)

                // 最大记录数
                let maxRecord = 0
                let maxStock = ''
                state.stocks.forEach(stock => {
                    if (maxRecord < state.raw_data[stock].length) {
                        maxRecord = state.raw_data[stock].length
                        maxStock = stock
                    }
                })

                let tableDataMap = new Map()
                // 设置初始值
                state.raw_data[maxStock].forEach(item => {
                    let tmp = {} 
                    tmp['date'] = item.date 
                    state.stocks.forEach(stock => {
                        tmp[stock] = null
                    })
                    tableDataMap.set(item.date,  tmp)
                })

                state.stocks.forEach(stock => {
                    state.raw_data[stock].forEach(item => {
                        tableDataMap.get(item.date)[stock] = item.lp
                    })
                })
                state.tableData = Array.from(tableDataMap.values())
            })
        }

        loadData()

        const tableColumnClassName = ({row, column, rowIndex, columnIndex}) => {

            let cell = column.label
            let levelPoint = row[cell]
            if (levelPoint) {
                return getLpClassName(levelPoint)
            }
        }

        const tableHeaderClick = (column, event) => {
            let stock = column.property
            openNewTableWeekDetail(stock)
        }
</script>

<style scope>
.lp-warning-row {
    background: rgb(215, 246, 248);
}
</style>